package com.mrd.gtimp.business.service;

import com.mrd.base.BaseBiz;
import com.mrd.gtimp.business.entity.Dictionary;
import com.mrd.gtimp.business.entity.DictionaryGroup;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

/**
 * Created by dark on 17/7/12.
 */
@Service
public class DictionaryBiz extends BaseBiz<Dictionary> {
    @Override
    @Resource
    @Qualifier("dictionaryDao")
    public void setBaseRepository(BaseRepository<Dictionary, String> baseRepository) {
        this.baseRepository = baseRepository;
    }

    @Resource
    private DictionaryGroupBiz dictionaryGroupBiz;

    public List<Dictionary> findByDictionaryGroupId(String dictionaryGroupId) {
        if (StringUtils.isEmpty(dictionaryGroupId)) {
            return null;
        }
        String hql = "select Dictionary from com.mrd.gtimp.business.entity.Dictionary Dictionary where Dictionary.dictionaryGroup.id='" + dictionaryGroupId + "'";
        List<Dictionary> dictionaryList = this.findEntityByHql(hql, 0, 0, null);
        return dictionaryList;
    }

    public List<Dictionary> findByDictionaryGroupCode(String code) {
        DictionaryGroup dictionaryGroup = dictionaryGroupBiz.findByCode(code);
        if (dictionaryGroup != null && StringUtils.isNotEmpty(dictionaryGroup.getId())) {
            return this.findByDictionaryGroupId(dictionaryGroup.getId());
        }
        return null;
    }
}
